package cn.wjdiankong.levideo.dialog;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.list.adapter.DragSortTabGridAdapter;
import cn.wjdiankong.levideo.utils.Const;
import cn.wjdiankong.levideo.utils.PreferenceUtil;
import cn.wjdiankong.levideo.utils.UIUtil;
import cn.wjdiankong.levideo.view.LevideoDialog;
import cn.wjdiankong.levideo.view.dragsortview.DragSortGridView;

public class DragTabSortDialog {
	
	private DragSortGridView dragSortGridView;
	private View parentView;
	private LevideoDialog dialog;
	private DragSortTabGridAdapter adapter;
	private Activity act;
	
	public final static String TAB_SORT = "sort_tab";
	
	@SuppressLint("InflateParams")
	public DragTabSortDialog(Activity act){
		this.act = act;
		if(parentView == null){
			parentView = LayoutInflater.from(act.getApplicationContext()).inflate(R.layout.dialog_drag_tab_sort_view, null);
		}
		if(dialog == null){
			dialog = new LevideoDialog(act, parentView, UIUtil.dip2px(act.getApplicationContext(), 280), 0);
		}
		dragSortGridView = (DragSortGridView) parentView.findViewById(R.id.dynamic_grid);
	}
	
	public void show(){
		List<Integer> dataList = getTagSortData();
		if(dataList == null){
			dataList = new ArrayList<Integer>();
			dataList.add(Const.VIDEO_TYPE_DOUYIN);
			dataList.add(Const.VIDEO_TYPE_KUAISHOU);
			dataList.add(Const.VIDEO_TYPE_HUOSHAN);
			dataList.add(Const.VIDEO_TYPE_MIAOPAI);
		}
		if(adapter == null){
			adapter = new DragSortTabGridAdapter(act, dataList);
		}
		dragSortGridView.setAdapter(adapter);
		dragSortGridView.setAppearingDrawable(R.drawable.video_screen_select_normal_bg);
		dragSortGridView.setDisappearingDrawable(R.drawable.video_screen_select_pressed_bg);
		parentView.findViewById(R.id.suer_btn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				saveTagSortData();
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	
	private void saveTagSortData(){
		if(adapter != null){
			List<Integer> sort = adapter.getDataList();
			if(sort != null && sort.size() == 4){
				String sortTab = sort.get(0) + "," + sort.get(1) + "," + sort.get(2) + "," + sort.get(3);
				PreferenceUtil.setValue(TAB_SORT, sortTab);
			}
		}
	}
	
	public static List<Integer> getTagSortData(){
		String str = (String) PreferenceUtil.getValue(TAB_SORT, "");
		if(!TextUtils.isEmpty(str)){
			try{
				String[] strAry = str.split(",");
				List<Integer> value = new ArrayList<Integer>(strAry.length);
				for(int i=0;i<strAry.length;i++){
					value.add(Integer.parseInt(strAry[i]));
				}
				return value;
			}catch(Exception e){
			}
		}
		return null;
	}

}
